import json

from django.http import HttpResponse

from datetime import date, datetime

class ComplexEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, date):
            return obj.strftime('%Y-%m-%d')
        else:
            return json.JSONEncoder.default(self, obj)


def to_response(status=0, response_type=HttpResponse, msg="", payload=None):
    json_str = json.dumps({
        "status": status,
        "msg": msg,
        "payload": payload
    }, cls=ComplexEncoder)
    return response_type(json_str)